package com.admin.service.impl;

import com.admin.entity.StationKeyRecharge;
import com.admin.mapper.StationKeyRechargeMapper;
import com.admin.service.IStationKeyRechargeService;
import com.admin.service.IStationService;
import com.admin.service.ITerminalKeyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author marisheng
 * @since 2019-11-21
 */
@SuppressWarnings("ALL")
@Service
public class StationKeyRechargeServiceImpl extends ServiceImpl<StationKeyRechargeMapper, StationKeyRecharge> implements IStationKeyRechargeService {

    @Autowired
    private StationKeyRechargeMapper stationKeyRechargeMapper;

    @Autowired
    private ITerminalKeyService terminalKeyService;

    @Autowired
    private IStationService stationService;

    // 通过站点外键Id查询 分页 密钥充值记录 同时开始时间和结束时间
    @Override
    public IPage queryKeyRecharge(int page, int limit, Map map) {
        Page p=new Page(page,limit);
        IPage result=stationKeyRechargeMapper.queryKeyRecharge(p,map);
        return result;
    }


    /**
     * 密钥充值
     * @param skr
     * @return
     */
    @Override
    public int insertTerminalKeys(StationKeyRecharge skr) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime time = LocalDateTime.now();
        String localTime = df.format(time);
        LocalDateTime ldt = LocalDateTime.parse(localTime, df);
        skr.setStationKeyRechargeTime(ldt);
        //新增密钥交易记录
        int n =terminalKeyService.insertTermianlKeySumAll(skr);
        int m=stationKeyRechargeMapper.insert(skr);
        //增加站点密钥余额
        int b=stationService.updateStationYue(skr);
        return  b;
    }


    /**
     * 站点密钥充值记录
     * @param page
     * @param limit
     * @param map
     * @return
     */
    @Override
    public IPage selectKeyRecharge(int page, int limit, Map map) {
        Page p = new Page(page, limit);
        return stationKeyRechargeMapper.selectKeyRecharge(p,map);
    }
}
